package presentation.data;

import javax.swing.JTable;

import business.SessionCache;
import business.exceptions.UIException;
import business.externalinterfaces.CustomerConstants;
import business.externalinterfaces.CustomerSubsystem;

public class DataUtil {
	public static String getSelectedValueFromTable(JTable table) throws UIException {
		int selectedRow = table.getSelectedRow();
    	if(selectedRow >= 0) {
    		return (String)table.getValueAt(selectedRow,0);
    	} else {
    		throw new UIException(MessagesUtil.SELECT_ROW_MSG);
    	}
	}
	
	public static String[] getSelectedRowFromTable(JTable table) throws UIException {
		int selectedRow = table.getSelectedRow();
		int numCols = table.getColumnCount();
		String[] returnValue = new String[numCols];
    	if(selectedRow >= 0) {
    		for(int i = 0; i < table.getColumnCount(); ++i) {
    			returnValue[i] = (String)table.getValueAt(selectedRow, i);
    		}
    		return returnValue;
    	} else {
    		throw new UIException(MessagesUtil.SELECT_ROW_MSG);
    	}
	}
	
	public static boolean custIsAdmin() {
		CustomerSubsystem cust = (CustomerSubsystem)SessionCache.getInstance().get(CustomerConstants.CUSTOMER);
        return (cust != null && cust.isAdmin());
	}
	
	public static boolean isLoggedIn() {
		return (Boolean)SessionCache.getInstance().get(CustomerConstants.LOGGED_IN);
	}
}
